package site.wanjiahao.handler.server;

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import site.wanjiahao.message.request.JoinGroupRequestMessage;
import site.wanjiahao.message.response.JoinGroupResponseMessage;
import site.wanjiahao.session.SessionFactory;

@ChannelHandler.Sharable
public class JoinGroupRequestMessageHandler extends SimpleChannelInboundHandler<JoinGroupRequestMessage> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, JoinGroupRequestMessage msg) throws Exception {
        boolean isJoin = SessionFactory.getGroupSession().join(msg.getGName(), ctx.channel());
        JoinGroupResponseMessage joinGroupResponseMessage = new JoinGroupResponseMessage(isJoin, isJoin ? "加入组成功" : "加入组失败");
        ctx.writeAndFlush(joinGroupResponseMessage);
    }
}
